package com.jf.demo;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.qr_codescan.MipcaActivityCapture;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.sys.android.util.FinalUntil;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

@ContentView(R.layout.my_already_order)
public class MyAlreadyOrder extends Activity {

	private int[] messImage = FinalUntil.messImage;
	private ArrayList<HashMap<String, Object>> remoteWindowItem;

	private String company[] = FinalUntil.company;

	@ViewInject(R.id.spinner1)
	private Spinner spinner1;

	@ViewInject(R.id.editText1)
	private EditText editText1;

	private SharedPreferences useShare;

	private String userId;

	private ListView vncListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);// xml视图加载的处理
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		ArrayAdapter<String> adspinner1 = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, company);
		adspinner1
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(adspinner1);
		useShare = getSharedPreferences("userInfo", MODE_PRIVATE);
		userId = useShare.getString("username", "");
		/*--------------------动态部分-----------------*/

		vncListView = (ListView) findViewById(android.R.id.list);
		alreadOrderList();
	}

	public void finishAlreadyOrder(View v) {
		this.finish();
	}

	public void scanTheBar(View v) {
		Intent intent = new Intent();
		intent.setClass(MyAlreadyOrder.this, MipcaActivityCapture.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityForResult(intent, 1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 1:
			if (resultCode == RESULT_OK) {
				Bundle bundle = data.getExtras();
				String code = bundle.getString("result");
				editText1.setText(code);
			}
			break;
		}
	}

	/**
	 * 查询按钮
	 */
	public void alreadFindClick(View v) {
		alreadOrderList();
	}

	/**
	 * 公用
	 */
	public void alreadOrderList() {
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("userID", userId);
		params.addQueryStringParameter("orderId", editText1.getText()
				.toString());

		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, FinalUntil.ServerAddress + "findUserDill",
				params, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("MyAlreadyOrder", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// 生成动态数组，加入数据
						remoteWindowItem = new ArrayList<HashMap<String, Object>>();
						try {
							JSONArray jsonarr = new JSONArray(arg0.result);
							for (int i = 0; i < jsonarr.length(); i++) {
								HashMap<String, Object> map = new HashMap<String, Object>();
								int imagea = jsonarr.getJSONObject(i).getInt(
										"brand");
								String str = jsonarr.getJSONObject(i)
										.getString("collData")
										+ "\n订单号:"
										+ jsonarr.getJSONObject(i).getString(
												"orderID")
										+ "\n物品:"
										+ jsonarr.getJSONObject(i).getString(
												"sendItems");
								map.put("ItemImage", messImage[imagea]);// 图像资源的ID
								map.put("ItemWinName", str);
								map.put("ItemCloseWin",
										android.R.drawable.ic_menu_close_clear_cancel);
								map.put("orderNum", jsonarr.getJSONObject(i)
										.getString("orderID"));
								map.put("cpmpanyName", jsonarr.getJSONObject(i)
										.getString("company"));
								map.put("sendItems", jsonarr.getJSONObject(i)
										.getString("sendItems"));
								remoteWindowItem.add(map);
							}

							MainmenuList listItemAdapter = new MainmenuList(
									MyAlreadyOrder.this, remoteWindowItem,
									"评 价");
							//
							vncListView.setAdapter(listItemAdapter);
							vncListView
									.setOnItemClickListener(new OnItemClickListener() {

										@Override
										public void onItemClick(
												AdapterView<?> arg0, View arg1,
												int arg2, long arg3) {
											evaluAtion(arg2);
											// Toast.makeText(MyAlreadyOrder.this,remoteWindowItem.get(arg2).get("ItemWinName").toString(),
											// Toast.LENGTH_LONG).show();
										}
									});
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				});
	}

	/**
	 * 评价
	 */
	public void evaluAtion(int arg) {
		final String cpmpanyName = remoteWindowItem.get(arg).get("cpmpanyName")
				.toString();
		final Intent intent = new Intent();
		intent.putExtra("dh", remoteWindowItem.get(arg).get("orderNum")
				.toString());
		intent.putExtra("gs", cpmpanyName);
		intent.putExtra("kdy", remoteWindowItem.get(arg).get("sendItems")
				.toString());
		intent.setClass(MyAlreadyOrder.this, MyOrderEvaluation.class);

		RequestParams params = new RequestParams();
		params.addQueryStringParameter("userID", userId);
		params.addQueryStringParameter("orderID", remoteWindowItem.get(arg)
				.get("orderNum").toString());

		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, FinalUntil.ServerAddress + "findAssess",
				params, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						Log.i("MyAlreadyOrder", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						try {
							JSONObject json = new JSONObject(arg0.result);

							if ("error".equals(json.getString("result"))) {
								startActivity(intent);
							} else {
								Intent intent = new Intent();
								intent.putExtra("cpmpanyName", cpmpanyName);
								intent.putExtra(
										"orderID",
										json.getJSONObject("result").getString(
												"orderID"));
								intent.putExtra(
										"assessGrade",
										json.getJSONObject("result").getString(
												"assessGrade"));
								intent.putExtra(
										"assessContent",
										json.getJSONObject("result").getString(
												"assessContent"));
								intent.putExtra(
										"assessData",
										json.getJSONObject("result").getString(
												"assessData"));
								intent.putExtra(
										"processingResults",
										json.getJSONObject("result").getString(
												"processingResults"));
								intent.setClass(MyAlreadyOrder.this,
										MyOrderEvaluationDetailed.class);
								startActivity(intent);
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}
}
